package com.ruoyi.common.utils;

import java.util.Random;

import org.apache.shiro.crypto.hash.Md5Hash;

public class EncryptUtil {
	
	/**
	 * 加密次数
	 */
	public static final Integer ITERATIONS = 1024;
	
	/**
	 * 盐值的位数
	 */
	public static final Integer PASS_NUM = 8;

	private static char[] saltArr = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();
	
	public static String getSalt(int sum){
		Random r = new Random();
		String result = "";
		for(int i = 0; i < sum; i++){
			int randomNum = r.nextInt(saltArr.length - 1);
			result += saltArr[randomNum];
		}
		return result;
	}
	
	/**
	 * 加密方法
	 * @param pass 密码
	 * @param salt 盐
	 * @param iterators 加密次数
	 * @return
	 */
	public static String getMd5Pass(String pass, String salt, int iterators){
		Md5Hash md5Hash = new Md5Hash(pass, salt, iterators);
		String result = md5Hash.toString();
		return result;
	}
	

  /**
   * 加密解密算法 执行一次加密，两次解密
   * @param inStr
   * @return
   */
  public static String convertMD5(String inStr){
  	char[] a = inStr.toCharArray();
  	for (int i = 0; i < a.length; i++){
  		a[i] = (char) (a[i] ^ 't');
  	}
  	String s = new String(a);
  	return s;
  }
  	 
	public static void main(String[] args) {
		String salt = EncryptUtil.getSalt(EncryptUtil.PASS_NUM);
		
		System.out.println(salt);
		String a=getMd5Pass("111111",salt,ITERATIONS);
		System.out.println(a);
	}
}
